var express = require("express");
const fs = require("fs");
var VideoRouter = express.Router();
const VedioController = require("../../controllers/video/VideoController");
const multer = require("multer");
const { v4: uuidv4 } = require("uuid");

const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    // 判断文件夹是否存在
    const dir = "public/video-temp/";
    if (!fs.existsSync(dir)) {
      fs.mkdirSync(dir);
    }
    cb(null, dir); //保存视频位置
  },

  filename: (req, file, cb) => {
    const uniqueSuffix = uuidv4();
    cb(null, file.fieldname + "-" + uniqueSuffix);
  },
});

const copeUpload = multer({ storage: storage }).single("video");

// 定义上传的 API 路由，并且使用上面的中间件
VideoRouter.post("/adminApi/uploadVideo", copeUpload, VedioController.upload);

module.exports = VideoRouter;
